﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.Persistent;
using System.Data;
using Jumpton.Data;
using Jumpton.SFA.Server.Entity;

namespace Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures
{
    public class MeasuresDetailManager : BusinessLogic
    {
        public MEASURE_DETAIL Load(int ID)
        {
            return (MEASURE_DETAIL)ObjectRow.Load(typeof(MEASURE_DETAIL), string.Format("this.ID={0}", ID));
        }

      
        public ObjectTable GetMeasuresDetailTable(decimal domainID,string where)
        {
            return new ObjectTable(typeof(MEASURE_DETAIL), "this.DOMAINID=" + domainID +" "+where);
        }
        /// <summary>
        /// 获取已存在项里的明细
        /// </summary>
        /// <param name="domainID">域ID</param>
        /// <param name="itemid">项ID</param>
        /// <returns></returns>
        public ObjectTable GetMeasuresDetailTableByItemID(decimal domainID, int itemid)
        {
            return new ObjectTable(typeof(MEASURE_DETAIL), "this.DOMAINID=" + domainID + " AND ID IN( SELECT MEASUER_DETAIL_ID FROM MEASURE_ITEM_DETAIL WHERE MEASUER_ITEMS_ID=" + itemid+")");
        }
        /// <summary>
        /// 获取不存在项里的明细
        /// </summary>
        /// <param name="domainID">域ID</param>
        /// <param name="itemid">项ID</param>
        /// <returns></returns>
        public ObjectTable GetOtherMeasuresDetailTableByItemID(decimal domainID, int itemid,string where)
        {
            return new ObjectTable(typeof(MEASURE_DETAIL), "this.DOMAINID=" + domainID + " AND ID NOT IN( SELECT MEASUER_DETAIL_ID FROM MEASURE_ITEM_DETAIL WHERE MEASUER_ITEMS_ID=" + itemid + ") "+where);
        }
        public bool Save(MEASURE_DETAIL details)
        {
            try
            {
                details.Connection.BeginTransaction(null);
                details.Save();
                details.Connection.CommitTransaction();

                return true;
            }
            catch (Exception ex)
            {
                details.Connection.RollbackTransaction();
                this.message = ex.Message;
                throw ex;
            }
        }

        public bool Save(MEASURE_ITEM_DETAIL itemdetails)
        {
            try
            {
                itemdetails.Connection.BeginTransaction(null);
                itemdetails.Save();
                itemdetails.Connection.CommitTransaction();

                return true;
            }
            catch (Exception ex)
            {
                itemdetails.Connection.RollbackTransaction();
                this.message = ex.Message;
                throw ex;
            }
        }


        public bool DelteItemDetailByItemID(decimal domainID, int itemid)
        {
            string strSql = " DELETE FROM MEASURE_ITEM_DETAIL WHERE DOMAIN_ID={0} AND MEASUER_ITEMS_ID={1}";
            strSql = string.Format(strSql,domainID,itemid);
            int res = SqlData.ExecuteSql(strSql);
            if (res >= 0)
            {
                return true;
            }
            return false;
        }
    }
}
